package com.sali.双指针;

/**
 * @author sali
 * @date 2025/5/13 21:24
 */
public class JZ_76 {

    public ListNode deleteDuplication(ListNode pHead) {
        //设置虚拟节点
        ListNode dummy = new ListNode(-1);
        dummy.next = pHead;

        //设置快慢指针
        ListNode slow = dummy;
        ListNode fast = pHead;

        //用于判断是否出现重复节点
        boolean flag = false;

        //特殊情况单独处理
        if(pHead == null){
            return pHead;
        }

        while(fast.next != null){

            //此时出现重复节点
            while(fast.next != null && fast.val == fast.next.val){
                flag = true;
                fast = fast.next;
            }

            //此时slow后面全是重复的节点，全删掉
            if(fast.next == null){
                slow.next = null;
                break;
            }

            fast = fast.next;
            if(flag){
                slow.next = fast;
                flag = false;
            }else{
                slow = slow.next;
            }
        }

        return dummy.next;
    }

}
